package com.example.wendao.view.ui.fragment;

import android.app.AlertDialog;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.wendao.App;
import com.example.wendao.R;
import com.example.wendao.common.PageUtils;
import com.example.wendao.model.entity.User;
import com.example.wendao.view.adapter.AgreementForumListAdapter;
import com.example.wendao.viewmodel.ForumViewModel;
import com.example.wendao.viewmodel.UserViewModel;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;


/**
 * A simple {@link Fragment} subclass.
 */
public class AgreeForumFragment extends Fragment {

    //private ActivityPagingTestBinding binding;
    private AgreementForumListAdapter adapter = new AgreementForumListAdapter();
    private ForumViewModel forumViewModel = null;
    private UserViewModel userViewModel = null;
    private String[] aforumId;
    private int[] arr;
    private String SQL = "";
    private User user;
    private RecyclerView recyclerView;
    private SwipeRefreshLayout srl;
    public AgreeForumFragment() {}
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_agreeforum, container, false);
        recyclerView = view.findViewById(R.id.rv_agreeforum);
        srl = view.findViewById(R.id.srl_test1);
        forumViewModel = new ViewModelProvider(App.getInstance()).get(ForumViewModel.class);
        userViewModel = new ViewModelProvider(App.getInstance()).get(UserViewModel.class);
        // 将user中的点赞id字段提取出来到arr数组中
        user = userViewModel.getCurrentUser();
        if (user == null) {
            AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
            builder.setTitle("提示");
            builder.setMessage("请您先登录再查看该内容");
            builder.setPositiveButton("确认", (dialog, which) -> {});
            builder.show();
            Navigation.findNavController(view).navigateUp();
        } else {
            if (user.getAforumId() != null){
                aforumId = user.getAforumId().split(",");
                for (int i = 0; i < aforumId.length; i++) {
                    try {
                        arr[i] = Integer.parseInt(aforumId[i]);
                    } catch (Exception e){}
                }
                if (arr != null){
                    //将arr数组中的id封装成一段sql语句
                    if(arr.length > 0) {
                        SQL += " '";
                        for(int i=0; i<arr.length-1; i++) {
                            SQL = SQL + arr[i] + "','";
                        }
                        SQL += arr[arr.length-1] + "'";
                    }
                }
            }
        }
        binds();
        forumViewModel.getForumsByForumIds(SQL).observe(this.getViewLifecycleOwner(), adapter::submitList);
        PageUtils.initRecyclerView(recyclerView, adapter, new LinearLayoutManager(this.getActivity()));
        return view;
    }
    private void binds() {
        // 模拟下拉刷新
        srl.setOnRefreshListener(() -> {
            srl.setRefreshing(true);
            Observable.just(0)
                    .delay(1, TimeUnit.SECONDS)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .doOnNext(i -> {
                        srl.setRefreshing(false);
                        adapter.submitList(null);
                        forumViewModel.getForumsByForumIds(SQL).observe(getActivity(), users -> adapter.submitList(users));
                    }).subscribe();
        });
    }
}
